package com.leetcode.Offer;

import java.util.Arrays;

public class JZ63 {
    public int maxProfit(int[] prices) {

        //动态规划
        //前n天最小价格
        if (prices==null||prices.length==0){
            return 0;
        }
        int minPrice = prices[0];
        int[] dp = new int[prices.length];
        Arrays.fill(dp, 0);
        for (int i = 1; i < prices.length; i++) {
            dp[i] = Math.max(dp[i - 1], prices[i] - minPrice);
            minPrice = Math.min(minPrice, prices[i]);
        }
        return dp[prices.length - 1];
    }

    public static void main(String[] args) {
        System.out.println(new JZ63().maxProfit(new int[]{7,6,4,3,1}));
    }
}
